package com.lianjia.crawler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lianjia.crawler.utils.DataLoader;
import com.lianjia.crawler.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 城市代码获取类，用于从链家网站获取城市代码
 */
public class GetCitys {
    private static final Logger logger = LoggerFactory.getLogger(GetCitys.class);
    private static final String URL = "https://www.lianjia.com/city/";
    private static final String REFERER = "https://cs.lianjia.com/";
    private static final Pattern CITY_CODE_PATTERN = Pattern.compile("https://([a-z]+)\\.lianjia\\.com/");

    /**
     * 从链家网站获取城市代码
     *
     * @return 城市代码Map
     */
    public static Map<String, String> getCityCodes() {
        String html = null;
        Map<String, String> cityCodeMap = null;

        while (cityCodeMap == null) {
            html = HttpUtils.getHtml(URL, DataLoader.getRandomUserAgent(), REFERER);
            cityCodeMap = extractCityCodes(html);
        }

        return cityCodeMap;
    }

    /**
     * 从HTML中提取城市代码
     *
     * @param html HTML内容
     * @return 城市代码Map
     */
    private static Map<String, String> extractCityCodes(String html) {
        if (html == null || html.trim().isEmpty()) {
            logger.warn("HTML内容为空");
            return null;
        }

        Map<String, String> cityCodeMap = new HashMap<>();

        try {
            Document doc = Jsoup.parse(html);
            Elements cityList = doc.select("div.city_province > ul > li");

            for (Element city : cityList) {
                Element link = city.selectFirst("a");
                if (link != null) {
                    String name = link.text();
                    String url = link.attr("href");
                    Matcher matcher = CITY_CODE_PATTERN.matcher(url);
                    if (matcher.find()) {
                        String code = matcher.group(1);
                        cityCodeMap.put(name, code);
                    }
                }
            }

            return cityCodeMap;
        } catch (Exception e) {
            logger.error("解析城市代码失败", e);
            return null;
        }
    }

    /**
     * 将城市代码保存到文件
     *
     * @param cityCodeMap 城市代码Map
     * @throws IOException IO异常
     */
    private static void saveCityCodes(Map<String, String> cityCodeMap) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        File dataDir = Paths.get("src", "main", "resources", "data").toFile();
        if (!dataDir.exists()) {
            dataDir.mkdirs();
        }
        File file = Paths.get("src", "main", "resources", "data", "CITY_CODE.json").toFile();
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, cityCodeMap);
        logger.info("城市代码已保存到 {}", file.getAbsolutePath());
    }

    public static void main(String[] args) {
        try {
            Map<String, String> cityCodeMap = getCityCodes();
            saveCityCodes(cityCodeMap);
        } catch (Exception e) {
            logger.error("获取城市代码失败", e);
        }
    }
} 